package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryAddDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult querycategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页，设置当前页面和每页记录数
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.getCategoryAll(categoryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public void addCategory(CategoryAddDTO CategoryAddDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(CategoryAddDTO,category);
        //默认开启状态
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.addCategory(category);

    }

    @Override
    public void updateCategoryStatus(Integer status, Long id) {
        Category category = categoryMapper.getById(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    @Override
    public void deleteCategory(Long id) {
        //判断当前分类是否启动
        Category category = categoryMapper.getById(id);
        if (category.getStatus() == StatusConstant.ENABLE) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        //判断是否有菜品在售卖
        List<DishVO> categoryById = dishMapper.getCategoryById(id);
        if (categoryById.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //当前分类关联了套餐,不能删除
        List<Setmeal> byCategoryId = setmealMapper.getByCategoryId(id);
        if (byCategoryId.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
        }


    @Override
    public void updateCategory(CategoryAddDTO categoryAddDTO) {
        Category category = categoryMapper.getById(categoryAddDTO.getId());
        BeanUtils.copyProperties(categoryAddDTO,category);
        categoryMapper.update(category);
    }

    @Override
    public List<Category> queryCategoryByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        category.setStatus(1);
        List<Category> list = categoryMapper.queryCategoryByType(category);
        return list;
    }
}
